﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_Them_CapNhat_TacGia : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void ThemMoi();
        public delegate void CapNhat();

        public event ThemMoi KhiThemMoi = null;
        public event CapNhat KhiCapNhat = null;

        public string Action = "";
        public string ID = null;

        TacGia_BUS ws;
        TacGia_DTO dto;

        public Form_Them_CapNhat_TacGia()
        {
            InitializeComponent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ws = new TacGia_BUS();
            dto = new TacGia_DTO();
            dto.Str_MaTacGia = txtMaTacGia.Text;
            dto.Str_TenTacGia = txtTenTacGia.Text;
            dto.Str_DiaChi = txtTenTacGia.Text;
            dto.D_NgaySinh = datNgaySinh.Value;
            dto.I_TrangThai = 0;//luc them moi thi dto o trang thai chua xoa
            if (Action.Equals("Insert"))
            {
                if (ws.ThemTacGia(dto) == 1)
                {
                    MessageBox.Show("Thêm thành công", "Thông báo");
                    if (KhiThemMoi != null)
                    {
                        KhiThemMoi();                        
                        txtMaTacGia.Text = ws.MaTacGiaTang();
                    }
                }
                else
                    MessageBoxEx.Show("Thêm thất bại", "Thông báo");
            }
            if (Action.Equals("Update"))
            {
                if (ws.CapNhatTacGia(dto) == 1)
                {
                    MessageBoxEx.Show("Cập nhật thành công", "Thông báo");
                    if (KhiCapNhat != null)
                    {
                        KhiCapNhat();
                        this.Close();
                    }
                }
                else
                    MessageBoxEx.Show("Cập nhật thất bại", "Thông báo");
            }      
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form_Them_CapNhat_TacGia_Load(object sender, EventArgs e)
        {
            SetBounds(Screen.GetWorkingArea(this).Width / 4, Screen.GetWorkingArea(this).Height / 4, Width, Height);
            //them nha xuat ban
            if (Action.Equals("Insert"))
            {
                ws = new TacGia_BUS();
                txtMaTacGia.Text = ws.MaTacGiaTang().ToString();
            }
            //cap nhat nha xuat ban
            if (Action.Equals("Update"))
            {
                ws = new TacGia_BUS();
                dto = ws.DanhSachTacGia_TheoMaTacGia(ID);
                txtMaTacGia.Text = dto.Str_MaTacGia;
                txtTenTacGia.Text = dto.Str_TenTacGia;
                txtDiaChi.Text = dto.Str_DiaChi;
                datNgaySinh.Value = dto.D_NgaySinh;
            }
        }
    }
}
